Agenda

  • Understand the structure of Rmarkdown documents

  • Understanding output formats in Rmarkdown

  • Understand coding in the Rmarkdown environment

  • Understand the basic patterns in markdown syntax

  • Building beautiful documents in Rmarkdown

What is Rmarkdown?

Structure of an Rmarkdown file

An Rmarkdown file is a file with a “.Rmd” extension and in Rstudio it looks as below

Rmarkdown Structure

Rmarkdown Structure

So, in general expect three different pieces in the Rmarkdown file

  1. YAML metadata (- - -) - details the output and some metadata related to the file

  2. Text and Formatting - Text interminced with symbols that help format text in the output document

  3. Code chunks and outputs (```) - Executable R code chunks with output, similar to code cells in jupyter notebook

Rmarkdown structure

Rmarkdown structure

Why Rmarkdown??

  • Rmarkdown documents help you format and output your analysis and R code with ouput in beautiful and sometimes interactive documents

YAML Headers in Rmarkdown

The YAML header defined by key: value pairs between a pair of - - -

Coding in Rmarkdown

Code Chunk:

#We write our code here

Code Chunk Options:

Chunk output can be customized with knitr options, arguments set in the {} of a chunk header:

  • include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks.
  • echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.
  • message = FALSE prevents messages that are generated by code from appearing in the finished file.
  • warning = FALSE prevents warnings that are generated by code from appearing in the finished.
  • fig.cap = “…” adds a caption to graphical results.

Lets write some R code now:

View the data

volcanic_data = read.csv("city.csv")

head(volcanic_data)
##   City.1 City.2 City.3 City.4 City.5
## 1     29     20     23     20     19
## 2     32     24     26     24     24
## 3     36     31     32     29     29
## 4     40     37     38     34     38
## 5     43     40     41     37     43
## 6     37     38     40     36     38
## Warning: package 'knitr' was built under R version 3.4.3
Volcanic data table in a neat way
City.1 City.2 City.3 City.4 City.5
29 20 23 20 19
32 24 26 24 24
36 31 32 29 29
40 37 38 34 38
43 40 41 37 43
37 38 40 36 38

What is the summary of the dataset

summary(volcanic_data)
##      City.1          City.2          City.3          City.4     
##  Min.   :29.00   Min.   :20.00   Min.   :23.00   Min.   :20.00  
##  1st Qu.:31.75   1st Qu.:27.00   1st Qu.:29.00   1st Qu.:26.25  
##  Median :34.50   Median :33.50   Median :34.00   Median :31.00  
##  Mean   :34.58   Mean   :31.58   Mean   :32.92   Mean   :29.75  
##  3rd Qu.:37.00   3rd Qu.:37.00   3rd Qu.:37.25   3rd Qu.:33.25  
##  Max.   :43.00   Max.   :40.00   Max.   :41.00   Max.   :37.00  
##      City.5     
##  Min.   :19.00  
##  1st Qu.:27.75  
##  Median :33.50  
##  Mean   :31.67  
##  3rd Qu.:36.50  
##  Max.   :43.00

Let us do a scatter plot

plot(volcanic_data$City.1, volcanic_data$City.2)

Basic patterns in Markdown Syntax

Markdown is a lightweight and easy-to-use syntax for styling and formatting all forms of text.

Think of HTML but much more cleaner and easier to read, even without the output

How does Rstudio understand R and markdown combined together?

How Rmarkdown works

This may sound complicated, but R Markdown makes it extremely simple by encapsulating all of the above processing into a single render function.

Markdown Syntax Basics

Bold, Italics and both in Markdown

  • Using Rmarkdown, we can bold text using Bold and Beautiful or Bold and Beautiful

  • Now, for italic we have, Smooth and Slant or Smooth and Silent

  • Combining both in single line Bold and Silent

Wiritng Sentences in Markdown

BLAH BALH BLAH!!!

Block Quotes in Markdown

Essentially, all models are wrong, but some are useful. - George Box

Unorderd and Orderd Lists in Markdown

Images in Markdown

Standard Deviation equation:

Latex in Markdown

  • Mardown gives us the ability to write math equations using latex, a cheat sheet is available at the following link

  • The standard deviation is given by the following formula

\[\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \overline{x})^2}\]

Some other commonly used symbols

  • Sigma: \(\sigma\)

  • Pi: \(\pi\)

  • Alpha: \(\alpha\)

  • Lambda: \(\lambda\)

  • Greater than or equal to: \(\ge\)

  • Plus or minus: \(\pm\)

Finally, Headings in Markdown

  • Markdown allows us to define headings using the hash symbol

  • The size of the heading gets smaller when the number of hashes increase

  • The table of contents are also generated using the number of hash symbols

  • “#” First level heading

  • “##” Second level sub heading

  • “###” Third level sub heading